<?php
namespace Tbf\Component\Excel;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Tbf\Component\Excel\Exception\ExcelException;
use Tbf\Component\FileSystem\FileSystemFunction;
use Tbf\Component\Http\SymfonyHttpFunction;

/**
 * excel相关处理,使用phpexcel这个库,
 * 此处概念简单,用户没有什么可以选择的...
 */
class Excel {
    /**
     * 数组转成excel文件,file_path是excel文件的地址
     * 如果有其他特殊要求,请另实现一个,不要改这个...
     * @param array $data [][]string
     * @param $file_path
     * @return string error
     */
    public static function fromArray(array $data,$file_path){
        $xls = new \PHPExcel();
        $sheet = $xls->getActiveSheet();
        $rowNum = 1;
        foreach($data as $row){
            $columnNum = -1;
            foreach($row as $value){
                $columnNum+=1;
                if ($value!==0 && empty($value)){
                    continue;
                }
                $sheet->setCellValueExplicitByColumnAndRow($columnNum,$rowNum,$value);
            }
            $rowNum+=1;
        }
        //关掉自动计算列宽度。
        foreach ($sheet->getColumnDimensions() as $colDimension) {
            $colDimension->setAutoSize(false);
        }
        $writer = \PHPExcel_IOFactory::createWriter($xls,'Excel5');
        $writer->save($file_path);
        return null;
    }

    /**
     * 将数组转换成一个输出excel的response
     * func(data [][]string)Response
     * @param array $data
     * @return Response
     */
    public static function getResponseFromArray(array $data){
        $resp = new StreamedResponse(function()use($data){
            Excel::fromArray($data,'php://output');
        });
        $file_name = date('Y-m-d_H-i-s').'.xls';
        SymfonyHttpFunction::addAttachmentHeader($resp,'application/vnd.ms-excel',$file_name);
        return $resp;
    }

    /**
     * excel文件转成数组
     * func(file_path string)([][]string,error)
     * 使用简单
     * 如果有其他特殊要求,请另实现一个,不要改这个...
     * @param $file_path
     * @return [][]string
     */
    public static function toArray($file_path){
        $xls = @\PHPExcel_IOFactory::load($file_path);
        $err = error_get_last();
        if ($err!==null){
            return array(null,'加载excel文件失败');
        }
        $sheet = $xls->getActiveSheet();
        $data = $sheet->toArray(null,false,false);
        return array($data,null);
    }

    /**
     * excel文件转成数组
     * 删除所有空行
     */
    public static function toArrayNoBlankLine($file_path){
        list($data,$err) = self::toArray($file_path);
        if ($err!==null){
            return array(null,$err);
        }
        foreach($data as $key=>$row){
            $blank_cell_num = 0;
            $total_cell_num = count($row);
            foreach($row as $k1=>$v2){
                if ($v2===null){
                    $blank_cell_num+=1;
                }
            }
            if ($total_cell_num===$blank_cell_num){
                unset($data[$key]);
            }
        }
        return array($data,$err);
    }
}